<?php
error_reporting ( E_ERROR   );
 
$port = 9502;
// var_dump($argv);
if(empty($argv[1]) == false){
    $port = intval($argv[1]);
}
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);

$client->on("connect", function(swoole_client $cli) {
    echo "connect======\n";
    //$cli->send("GET / HTTP/1.1\r\n\r\n");
});
$client->on("receive", function($cli, $data){
    echo "Receive: $data\n";
//     $cli->send(str_repeat('A', 100)."\n");
//     sleep(1);
});
$client->on("error", function($cli){
    echo "error=====\n";
});
$client->on("close", function($cli){
    echo "Connection close====\n";
});
$client->connect('127.0.0.1', 9502);

swoole_timer_tick(3000, function($id) {
    global $client;
    global $port;
    $data = array('host'=>'127.0.0.1', 'port'=>$port, 'time'=>time(), 'datetime'=>date("Y-m-d H:i:s"));

    echo "client->errCode======" . $client->errCode." client->isConnected==={$client->isConnected()}\n";

//     if($client->isConnected() == false){  // $client->isConnected() == false && 
//         echo "connect retry \n";
//         $client->connect('127.0.0.1', 9502, -1);
//     }
//     else{
//         $send_data = json_encode($data)."\r\n";
//         try{
//             $client->send($send_data);
//             $client->close();
//         }
//         catch(Exception $ex){
//             $client->close();
//             echo "send error======" . $ex->getTrace()."\n";
//         }
// //         $rec_data = $client->recv();
// //         echo $rec_data;
//     }
   
});
// $client->close();